<?php

namespace AppBundle\Controller;

use AdminBundle\Entity\HuobiAlertSender;
use AdminBundle\Entity\HuobiAlertTimestamp;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

abstract class HuobiBaseController extends Controller
{
    /**
     * 数据JSON打包
     * @param $code
     * @param $message
     * @param $data
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    protected function apiResult($code, $message, $data)
    {
        return $this->json(['code' => $code, 'message' => $message, 'time' => time(), 'data' => $data]);
    }

    protected function sendMail($subject, $content, $options = [])
    {
        $em = $this->getDoctrine()->getManager();
        // 随机取出一个发送邮件用户
        $alertSenders = $em->getRepository('AdminBundle:HuobiAlertSender')->getSender();

        if (empty($alertSenders)) {
            $sender = ['addr' => '18025379@qq.com', 'name' => '客服:大成', 'password' => 'wdfzplrejuegcadg'];
//            return $this->json(['code' => 2, 'message'=>'no sender','time' => time(), 'data' => 0]);
        } else {
            $sender = $alertSenders[0];
        }
        // 取出接收邮件的收件人
        if (isset($options['recipient'])) {
            $alertRecipients = $em->getRepository('AdminBundle:HuobiAlertRecipients')->findBy(['note' => $options['recipient']]);
        } else {
            $alertRecipients = $em->getRepository('AdminBundle:HuobiAlertRecipients')->findAll();
        }
        if ($alertRecipients == null) {
            return -1;
        }
        // 整理收件人格式数组
        $recipients = array();
        foreach ($alertRecipients as $alertRecipient) {
            $recipients[$alertRecipient->getEmail()] = $alertRecipient->getName();
        }

        // 发送邮件
        $result = self::mailFromQQTo($sender, $subject, $recipients, $content);

        return $result;
    }

    /**
     * 通过QQ邮箱发送邮件
     * @param $from
     * @param $subject
     * @param $to
     * @param $content
     * @return int
     */
    static public function mailFromQQTo($from, $subject, $to, $content)
    {
        $message = \Swift_Message::newInstance()
            ->setFrom($from['addr'], $from['name'])
            ->setSubject($subject)
            ->setTo($to)
            ->setBody($content, 'text/html');;

        $transport = (new \Swift_SmtpTransport('smtp.example.org', 25))
            ->setUsername($from['addr'])
            ->setPassword($from['password'])
            ->setEncryption('ssl')
            ->setHost('smtp.qq.com')
            ->setPort(465);

        // Create the Mailer using your created Transport
        $mailer = new \Swift_Mailer($transport);

//         $mailer = $this->get('mailer');
//         or
//         $mailer = $this->get('swiftmailer.mailer');
//        $mailer = $this->get('swiftmailer.mailer.alert_mailer');
        return $mailer->send($message);
    }
}
